//
//  SBTabBarController.m
//  Weibo
//
//  Created by 宋洋 on 15/3/3.
//  Copyright (c) 2015年 suibian. All rights reserved.
//

#import "SBTabBarController.h"
#import "SBTabBar.h"

@interface SBTabBarController ()

@end

@implementation SBTabBarController

- (instancetype)init
{
    if (self = [super init]) {
        DDLogDebug(@"%d %s", __LINE__ , __func__);
    }
    return self;
}
// init方法内部会调用initWithNibName方法, 所以在自定义控制器时可以把初始化的代码放到initWithNibName方法中, 以后无论是通过init方法还是通过initWithNibName都可以初始化
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        DDLogDebug(@"%d %s", __LINE__ , __func__);
        // 4.创建子控制器
        
        UIViewController *home = [self addControllerWithClass:[UIViewController class] title:@"首页" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
        UIViewController *message = [self addControllerWithClass:[UIViewController class] title:@"消息" image:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected"];
        
//        UIViewController *other = [self addControllerWithClass:[UIViewController class] title:@"" image:@"" selectedImage:@""];
        
        UIViewController *discover = [self addControllerWithClass:[UIViewController class] title:@"发现" image:@"tabbar_discover" selectedImage:@"tabbar_discover_selected"];
        UIViewController *profile = [self addControllerWithClass:[UIViewController class] title:@"我" image:@"tabbar_profile" selectedImage:@"tabbar_profile_selected"];
        self.viewControllers = @[home, message, discover, profile];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
//    DDLogDebug(@"subviews = %@",self.tabBar.subviews);
    
    // 1.创建自定义SBTabBar
    SBTabBar *tabBar = [[SBTabBar alloc] init];
    DDLogDebug(@"替换前 %@",self.tabBar);
    
    // 2.用自定义的SBTabBar替换系统的TabBar
//    self.tabBar = tabBar;
    //KVC: KVC的实现原理是基于运行时,它可以在运行时动态的修改属性
    [self setValue:tabBar forKeyPath:@"tabBar"];
    DDLogDebug(@"替换后 %@",self.tabBar);
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    DDLogDebug(@"subviews = %@",self.tabBar.subviews);
}

/**
 *  根据一个类创建一个控制器
 *
 *  @param class         控制器对应的类
 *  @param title         标题
 *  @param image         默认状态的图片
 *  @param selectedImage 选择状态图片
 *
 *  @return 创建好的控制器
 */
- (UIViewController *)addControllerWithClass:(Class)class title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    /*
     注意: 不是所有的控制器都是通过init方法初始化的
     */
    UIViewController *vc = [[class alloc] init];
    return [self addControllerWithVc:vc title:title image:image selectedImage:selectedImage];
}

/**
 *  根据一个创建好的控制器初始化控制器
 *
 *  @param vc            控制器
 *  @param title         标题
 *  @param image         默认图片
 *  @param selectedImage 选中图片
 *
 *  @return 初始化之后的控制器
 */
- (UIViewController *)addControllerWithVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:image];
    /*
     iOS7需要通过代码告诉系统不要用tabBar.tintColor来渲染选中图片
     */
    // 1.创建图片
    UIImage *newImage =  [UIImage imageNamed:selectedImage];
    // 2.告诉系统原样显示
    newImage = [newImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 3.设置图片
    vc.tabBarItem.selectedImage = newImage;
    vc.view.backgroundColor = [UIColor greenColor];
//    vc.view.backgroundColor = IWRandomColor;
    
    // 设置tabBarButton的标题颜色
    // 设置文字的属性有一个规律:NSXXXXXAttributeName
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];
    return vc;
}
@end
